const CryptoJS = require("crypto-js")
const sm4 = require("sm-crypto").sm4


function r(e, t) {
    var n, a, i = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split(""), r = [];
    if (t = t || i.length,
        e)
        for (n = 0; n < e; n++)
            r[n] = i[0 | Math.random() * t];
    else
        for (r[8] = r[13] = r[18] = r[23] = "-",
                 r[14] = "4",
                 n = 0; n < 36; n++)
            r[n] || (a = 0 | 16 * Math.random(),
                r[n] = i[19 == n ? 3 & a | 8 : a]);
    return r.join("")
}

function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    const seconds = String(date.getSeconds()).padStart(2, '0');

    return `${year}${month}${day}${hours}${minutes}${seconds}`;
}


function o(e, t) {
    var n = CryptoJS.HmacSHA256(e, t).toString();
    return n
}

function c(e, t) {
    i = sm4.encrypt(e, t);
    return i
}

function u(e) {
    for (var t = "", n = 0; n < e.length; n++)
        "" === t ? t = e.charCodeAt(n).toString(16) : t += e.charCodeAt(n).toString(16);
    return t
}

g = r(16, 61)
p = g.substring(0, 8) + "dt!Ppb9t"
t_data = {
    "client_id": "saewfebs7ceb4weea4b7w7962b4s3r2b",
    "redirect_uri": "https://etax.dalian.chinatax.gov.cn/cas/loginSubmit?service=https://etax.dalian.chinatax.gov.cn/cas/login?service=https://etax.dalian.chinatax.gov.cn/shiro-cas",
    "account": "1313",
    "password": "1313"
}
f = JSON.stringify(t_data)
var timestamp = formatDate(new Date())
result_ss = c(f, u(p))

signature = o("0" + '2' + result_ss + timestamp + "HMacSHA256", g)
console.log(signature)